package com.frame.work.frame.deal.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frame.work.common.constant.Const;
import com.frame.work.common.util.YmlStatic;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblFingerprint;
import com.frame.work.mapper.TblFingerprintMapper;
import com.frame.work.service.impl.JpushService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wjming
 * @ClassName: WeakCurrentDeviceUtil
 * @Description:弱电设备通用工具类
 * @Date 2021/1/28 11:39
 */
public class WeakCurrentDeviceUtil {
    public static Boolean SaveLogAndSendJpush(String deviceMac, String gatewayMac, String content, String type) {
        Boolean isSend = false;
        //获取基础数据
        List<DeviceAssociatedInformation> deviceUserMessage = DeviceRelationMessageUtil.getDeviceUserMessage(deviceMac);
        if (CollectionUtils.isEmpty(deviceUserMessage)) {
            return false;
        }
        DeviceAssociatedInformation deMsgRelation = deviceUserMessage.get(0);
        //极光推送Bean操作
        JpushService jPushService = SpringUtil.getBean(JpushService.class);
        String homeName = StringUtils.isNotEmpty(deMsgRelation.getHomeName()) ? deMsgRelation.getHomeName() : "";
        String deviceName = StringUtils.isNotEmpty(deMsgRelation.getDeviceName()) ? deMsgRelation.getDeviceName() : "设备";
        content = homeName + deviceName + content;
        try {
            switch (deMsgRelation.getPushType()) {
                //都推送
                case "1":
                    isSend = true;
                    break;
                //布防推送
                case "2":
                    if (StringUtils.equals("01", deMsgRelation.getDefendStatus())) {
                        isSend = true;
                    }
                    break;
                //撤防推送
                case "3":
                    if (StringUtils.equals("02", deMsgRelation.getDefendStatus())) {
                        isSend = true;
                    }
                    break;
                //不推送
                case "4":
                    isSend = false;
                    break;
                default:
                    break;
            }
            SaveLogUtil.saveLog(gatewayMac, deviceMac, content, type, deMsgRelation.getUserId());
            if (isSend) {
                jPushService.sendNoSave("", content, type, deMsgRelation.getSourceId(), Const.MsgType.MSG_WARNING_TYPE,
                        deMsgRelation.getDeviceId(), deMsgRelation.getUserId(), deMsgRelation.getSourceType(), deMsgRelation.getOrganizationCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * @Description:保存指纹信息
     * @param: data
     * @param: deviceUserMessage
     * @param: getKeyId
     * @param: name
     * @param: userType
     * @return: com.frame.work.entity.TblFingerprint
     * @author:wjming
     * @Date:2021/2/5 9:40
     */
    public static TblFingerprint openTheDoorByKey(DeviceAssociatedInformation deviceUserMessage, String userNumber, String name, String idType) {
        //钥匙类型
        TblFingerprintMapper tblFingerprintMapper = SpringUtil.getBean(TblFingerprintMapper.class);
        //先查询指纹表里有没有这个指纹
        QueryWrapper<TblFingerprint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceUserMessage.getDeviceId()).eq("number", userNumber).eq("id_type", idType);
        List<TblFingerprint> tblFingerprints = tblFingerprintMapper.selectList(queryWrapper);

        if (tblFingerprints.size() == 0) {
            Integer integer = tblFingerprintMapper.selectCount(Wrappers.<TblFingerprint>lambdaQuery()
                    .eq(TblFingerprint::getIdType, idType).eq(TblFingerprint::getDeviceId, deviceUserMessage.getDeviceId()));
            //指纹锁表里新增数据
            TblFingerprint tblFingerprint = new TblFingerprint();
            tblFingerprint.setDeviceId(deviceUserMessage.getDeviceId());
            tblFingerprint.setNumber(userNumber);
            tblFingerprint.setName(name + "指纹" + (integer + 1) + "");
            tblFingerprint.setIdType(idType);
            tblFingerprintMapper.insert(tblFingerprint);
        }
        return tblFingerprintMapper.selectOne(Wrappers.<TblFingerprint>lambdaQuery()
                .eq(TblFingerprint::getDeviceId, deviceUserMessage.getDeviceId())
                .eq(TblFingerprint::getNumber, userNumber).eq(TblFingerprint::getIdType, idType));
    }

    /**
     * @Description:场景触发器
     * @param: fingerprint
     * @return: void
     * @author:wjming
     * @Date:2021/2/5 10:14
     */
    public static void sceneTrigger(TblFingerprint fingerprint) {
        if (fingerprint.getSceneId() != null) {
            Map<String, Object> map = new HashMap<>(3);
            //删除一次性密码数据
            map.put("dealType", "fingerprintLinkageScene");
            //sceneId
            map.put("sceneId", fingerprint.getSceneId());
            RabbitTemplate rabbitTemplate = SpringUtil.getBean(RabbitTemplate.class);
            rabbitTemplate.convertAndSend(YmlStatic.getGeneralQueueProcessing(), map);
        }
    }
}
